package com.learn.core.controller.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.learn.common.annotation.Log;
import com.learn.common.annotation.UserLoginToken;
import com.learn.common.enums.BusinessType;
import com.learn.common.enums.OperatorType;
import com.learn.common.enums.ResultEnum;
import com.learn.common.utils.JwtUtil;
import com.learn.common.utils.PageResult;
import com.learn.common.utils.ResultUtil;
import com.learn.common.utils.StringUtils;
import com.learn.common.utils.result.CommonResult;
import com.learn.core.domain.LRotatePictures;
import com.learn.core.dto.LRotatePicturesDTO;
import com.learn.core.service.ILRotatePicturesService;
import com.learn.framework.web.controller.WebController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName LRotatePicturesController
 * @date 2021.07.06 13:34
 * @describe 轮播图
 */
@Api("轮播图")
@RestController
@RequestMapping("/manager/rotate")
public class LRotatePicturesController extends WebController<LRotatePictures> {

    @Autowired
    private ILRotatePicturesService ilRotatePicturesService;

    //列表
    @ApiOperation(value = "获取列表")
    @UserLoginToken
    @PostMapping("/list")
    public PageResult<LRotatePictures> list(@RequestBody LRotatePicturesDTO lRotatePicturesDTO){
        try {
            if (lRotatePicturesDTO==null||lRotatePicturesDTO.getPage()==null||lRotatePicturesDTO.getRows()==null){
                return new PageResult<>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lRotatePicturesDTO.getPage(),lRotatePicturesDTO.getRows());
            QueryWrapper<LRotatePictures> queryWrapper = new QueryWrapper<>();
            if(StringUtils.isNotEmpty(lRotatePicturesDTO.getRotateTop()))
                queryWrapper.eq("rotate_top",lRotatePicturesDTO.getRotateTop());
            queryWrapper.orderByAsc("rotate_seq");
            Page<LRotatePictures> LRotatePictures = (Page<LRotatePictures>)ilRotatePicturesService.list(queryWrapper);
            if (CollectionUtils.isEmpty(LRotatePictures)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            // 返回
            return new PageResult(LRotatePictures.getTotal(), LRotatePictures, ResultEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }

    //更新
    @ApiOperation(value = "更新")
    @PostMapping("/edit")
    @UserLoginToken
    @Log(title = "更新轮播图", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> edit(@RequestBody LRotatePictures lRotatePictures, HttpServletRequest request){
        // token
        String token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        if(lRotatePictures!=null){
            lRotatePictures.setUpdateTime(new Date());
            lRotatePictures.setUpdateUser(userId);
            if(ilRotatePicturesService.updateById(lRotatePictures)){
                return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
            }
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //新增
    @ApiOperation(value = "添加")
    @PostMapping("/add")
    @UserLoginToken
    @Log(title = "添加轮播图", businessType = BusinessType.INSERT, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> add(@RequestBody LRotatePictures lRotatePictures, HttpServletRequest request){
        // token
        String token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if(lRotatePictures!=null) {
                if (StringUtils.isEmpty(lRotatePictures.getPicturesUrl())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_PICTURES_URL);
                }
                lRotatePictures.setCreateTime(new Date());
                lRotatePictures.setCreateUser(userId);
                if (ilRotatePicturesService.save(lRotatePictures)) {
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return  (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }


    //删除
    @ApiOperation(value = "删除")
    @PostMapping("/del")
    @UserLoginToken
    @Log(title = "删除轮播图", businessType = BusinessType.DELETE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> del(@RequestBody LRotatePictures lRotatePictures){
        try {
            if(lRotatePictures!=null){
                if(lRotatePictures.getRotateId()!=null){
                    QueryWrapper<LRotatePictures> queryWrapper = new QueryWrapper<LRotatePictures>();
                    queryWrapper.in(true, "rotate_id", lRotatePictures.getRotateId());
                    if(ilRotatePicturesService.remove(queryWrapper)){
                        return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
                    }
                }
            }
        }catch (Exception e)
        {
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }
}
